﻿namespace UnityEditor
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.CompilerServices;
    using UnityEngine;

    internal class LabelInspector
    {
        [CompilerGenerated]
        private static Func<PopupList.ListElement, bool> <>f__am$cache4;
        [CompilerGenerated]
        private static Func<PopupList.ListElement, string> <>f__am$cache5;
        [CompilerGenerated]
        private static Func<PopupList.ListElement, bool> <>f__am$cache6;
        [CompilerGenerated]
        private static Func<PopupList.ListElement, string> <>f__am$cache7;
        [CompilerGenerated]
        private static Func<PopupList.ListElement, GUIContent> <>f__am$cache8;
        private static UnityEngine.Object currentAsset;
        private static bool currentChanged;
        private static PopupList.InputData m_AssetLabels;
        private static int m_MaxShownLabels = 10;

        public static void AssetLabelListCallback(PopupList.ListElement element)
        {
            element.selected = !element.selected;
            currentChanged = true;
            SaveLabels();
            InspectorWindow.RepaintAllInspectors();
        }

        public static void InitLabelCache(UnityEngine.Object asset)
        {
            if ((asset != currentAsset) || (m_AssetLabels == null))
            {
                string[] labels = AssetDatabase.GetLabels(asset);
                Dictionary<string, float> allLabels = AssetDatabase.GetAllLabels();
                PopupList.InputData data = new PopupList.InputData {
                    m_AllowCustom = true,
                    m_OnSelectCallback = new PopupList.OnSelectCallback(LabelInspector.AssetLabelListCallback),
                    m_MaxCount = 15,
                    m_SortAlphabetically = true
                };
                m_AssetLabels = data;
                <InitLabelCache>c__AnonStorey3C storeyc = new <InitLabelCache>c__AnonStorey3C();
                using (Dictionary<string, float>.Enumerator enumerator = allLabels.GetEnumerator())
                {
                    while (enumerator.MoveNext())
                    {
                        storeyc.pair = enumerator.Current;
                        PopupList.ListElement element = m_AssetLabels.NewOrMatchingElement(storeyc.pair.Key);
                        if (element.filterScore < storeyc.pair.Value)
                        {
                            element.filterScore = storeyc.pair.Value;
                        }
                        element.selected = Enumerable.Any<string>(labels, new Func<string, bool>(storeyc.<>m__72));
                    }
                }
            }
            currentAsset = asset;
            currentChanged = false;
        }

        public static void OnDisable()
        {
            SaveLabels();
        }

        public static void OnLabelGUI(UnityEngine.Object asset)
        {
            InitLabelCache(asset);
            GUIStyle tagTextField = EditorStyles.tagTextField;
            float maxHeight = EditorStyles.tagTextFieldButton.CalcHeight(GUIContent.none, 20f);
            float num2 = 20f;
            Rect rect = GUILayoutUtility.GetRect(0f, 10240f, 0f, maxHeight);
            rect.width -= num2;
            EditorGUIUtility.LookLikeControls();
            EditorGUILayout.BeginHorizontal(new GUILayoutOption[0]);
            if (<>f__am$cache6 == null)
            {
                <>f__am$cache6 = i => i.selected;
            }
            if (<>f__am$cache7 == null)
            {
                <>f__am$cache7 = i => i.text.ToLower();
            }
            if (<>f__am$cache8 == null)
            {
                <>f__am$cache8 = i => i.m_Content;
            }
            IEnumerator<GUIContent> enumerator = Enumerable.Select<PopupList.ListElement, GUIContent>(Enumerable.OrderBy<PopupList.ListElement, string>(Enumerable.Where<PopupList.ListElement>(m_AssetLabels.m_ListElements, <>f__am$cache6), <>f__am$cache7), <>f__am$cache8).Take<GUIContent>(m_MaxShownLabels).GetEnumerator();
            try
            {
                while (enumerator.MoveNext())
                {
                    GUIContent current = enumerator.Current;
                    Rect rect2 = GUILayoutUtility.GetRect(current, EditorStyles.tagTextField);
                    if ((Event.current.type == EventType.Repaint) && (rect2.xMax >= rect.xMax))
                    {
                        goto Label_014A;
                    }
                    GUI.Label(rect2, current, tagTextField);
                }
            }
            finally
            {
                if (enumerator == null)
                {
                }
                enumerator.Dispose();
            }
        Label_014A:
            GUILayout.FlexibleSpace();
            Rect position = GUILayoutUtility.GetRect(20f, 20f, (float) 20f, (float) 20f);
            position.x = rect.xMax;
            if (EditorGUI.ButtonMouseDown(position, GUIContent.none, FocusType.Passive, EditorStyles.tagTextFieldButton))
            {
                if (PopupList.isOpen)
                {
                    PopupList.CloseList();
                }
                if (PopupList.ShowAtPosition(new Vector2(position.x, position.y), m_AssetLabels, PopupList.Gravity.Bottom))
                {
                    GUIUtility.ExitGUI();
                }
                HandleUtility.Repaint();
            }
            EditorGUILayout.EndHorizontal();
        }

        public static void OnLostFocus()
        {
            SaveLabels();
        }

        public static void SaveLabels()
        {
            if ((currentChanged && (m_AssetLabels != null)) && (currentAsset != null))
            {
                if (<>f__am$cache4 == null)
                {
                    <>f__am$cache4 = i => i.selected;
                }
                if (<>f__am$cache5 == null)
                {
                    <>f__am$cache5 = i => i.text;
                }
                AssetDatabase.SetLabels(currentAsset, Enumerable.Select<PopupList.ListElement, string>(Enumerable.Where<PopupList.ListElement>(m_AssetLabels.m_ListElements, <>f__am$cache4), <>f__am$cache5).ToArray<string>());
                currentChanged = false;
            }
        }

        [CompilerGenerated]
        private sealed class <InitLabelCache>c__AnonStorey3C
        {
            internal KeyValuePair<string, float> pair;

            internal bool <>m__72(string label)
            {
                return string.Equals(label, this.pair.Key, StringComparison.OrdinalIgnoreCase);
            }
        }
    }
}

